<?php

namespace app\common\model\mysql;

use app\common\business\DataAuth;
use think\Model;

class SysRole extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 新增前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeInsert($model){
        $userData = (new DataAuth())->getUserDetails();
        $model->create_by = $userData['userName'];
    }

    /**
     * 更新前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeUpdate($model)
    {
        $userData = (new DataAuth())->getUserDetails();
        $model->update_by = $userData['userName'];
    }

    /**
     * 查询角色列表
     * @param $where
     * @param $field
     * @return SysPost[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalRoleList($where, $field,$limit,$page){
        $order = [
            "role_sort" =>"asc",
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    public function getNormalRoleNotPageList($where, $field){
        $order = [
            "role_sort" =>"asc",
        ];
        $result = $this->where($where)->field($field)->order($order)->select();
        return $result;
    }

    public function getNormalRoleFiledArr($where, $field){
        $order = [
            "role_sort" =>"asc",
        ];
        $result = $this->where($where)->order($order)->column($field);
        return $result;
    }

    /**
     * 根据角色名称获取角色数据
     * @param $name
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRoleByNameFind($role_name,$role_id){
        if(empty($role_name)){
            return false;
        }
        $where = [
            "role_name" =>$role_name,
            "del_flag" => "0",
        ];
        if($role_id){
            $result = $this->where("role_id", "<>", $role_id)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }

    /**
     * 根据角色权限字符获取角色数据
     * @param $name
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRoleByKeyFind($role_key,$role_id){
        if(empty($role_key)){
            return false;
        }
        $where = [
            "role_key" =>$role_key,
            "del_flag" => "0",
        ];
        if($role_id){
            $result = $this->where("role_id", "<>", $role_id)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }

    /**
     * 根据$id获取岗位数据
     * @param $id
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRoleByIdFind($id){
        if(empty($id)){
            return false;
        }
        $where = [
            "role_id" =>$id,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    public function getRoleDataScopeModel($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['role_id','in',$ids]
        ];
        $result = $this->where($where)->column('data_scope');
        return $result;
    }
    public function updateRoleById($id,$data){
        if(empty($id) || empty($data)){
            return false;
        }
        $where = [
            "role_id" =>$id,
        ];
        $result = $this->where($where)->find()->save($data);
        return $result;
    }

    public function DelRoleIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['role_id', 'in', $ids]
        ];
        $data = [
            'del_flag'=>'2'
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    public function changeStatusRoleModel($roleId,$status){
        if(empty($roleId)){
            return false;
        }
        $where = [
            ['role_id', '=', $roleId]
        ];
        $data = [
            'status'=>$status
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }
}
